package com.youshang520i.zookeeperdiscoveryconsumer;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import java.net.URI;
import java.util.List;
import java.util.Random;

@EnableDiscoveryClient
@RestController
@Slf4j
@SpringBootApplication
public class ZookeeperDiscoveryConsumer {

    public static void main(String[] args) {
        SpringApplication.run(ZookeeperDiscoveryConsumer.class);
    }

    @Bean
    @LoadBalanced
    @Qualifier("loadBalancedRestTemplate")
    public RestTemplate loadBalancedRestTemplate() {
        return new RestTemplate();
    }

    @Bean
    public RestTemplate restTemplate() {
        return new RestTemplate();
    }

    // 1. 使用带负载均衡功能的 RestTemplate 访问服务
    @Autowired
    @Qualifier("loadBalancedRestTemplate")
    private RestTemplate loadBalancedRestTemplate;

    @Autowired
    @Qualifier("restTemplate")
    private RestTemplate restTemplate;

    // 2. 使用 DiscoveryClient 访问服务
    @Autowired
    private DiscoveryClient discoveryClient;

    private static final String ZOOKEEPER_DISCOVERY_PROVIDER_NAME = "zookeeper-discovery-provider";
    private static final String ZOOKEEPER_DISCOVERY_PROVIDER_URL = "http://zookeeper-discovery-provider";

    @RequestMapping("/loadBalancedRestTemplate")
    public String invokeWithRestTemplate() {
        return loadBalancedRestTemplate.getForObject(ZOOKEEPER_DISCOVERY_PROVIDER_URL, String.class);
    }

    @RequestMapping("/discoveryClient")
    public String invokeWithDiscoveryClient() throws Exception {
        List<ServiceInstance> instances = discoveryClient.getInstances(ZOOKEEPER_DISCOVERY_PROVIDER_NAME);
        log.info("serviceList -> {}", instances);
        ServiceInstance serviceInstance = instances.get(0);

        String host = serviceInstance.getHost();
        String instanceId = serviceInstance.getInstanceId();
        String serviceId = serviceInstance.getServiceId();
        String scheme = serviceInstance.getScheme();

        log.info("host : "+ host + " instanceId: "+ instanceId + " serviceId :" + serviceId + " scheme : "+ scheme);




        String serviceUrl = instances.get(new Random().nextInt(instances.size())).getUri().toURL().toString();
        log.info("serviceUrl -> {}", serviceUrl);
        return restTemplate.getForObject(serviceUrl, String.class);
    }
}
